package com.coder.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.ByteArrayInputStream;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.coder.utils.SecurityCode;
import com.coder.utils.SecurityImage;

@SuppressWarnings("serial")
public class SecurityCodeImageAction extends ActionSupport implements SessionAware{
    
	//the session of Map in Struts2
    private Map<String, Object> session;
    
    //image stream
    private ByteArrayInputStream imageStream;

    public String execute() throws Exception {
    	
    	//in the "Hard Model", ignore the lower case or up case
    	//String securityCode = SecurityCode.getSecurityCode(4,SecurityCodeLevel.Hard, false).toLowerCase();
        
    	//get the default format of security code
        String securityCode = SecurityCode.getSecurityCode();
        imageStream = SecurityImage.getImageAsInputStream(securityCode);
        
        //put security code into session
        session.put("SESSION_SECURITY_CODE", securityCode);
        
        return SUCCESS;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
    
    public ByteArrayInputStream getImageStream() {
        return imageStream;
    }

    public void setImageStream(ByteArrayInputStream imageStream) {
        this.imageStream = imageStream;
    }
}